Skip to content

feat(telegram): networking isolation and sticky IPv4 fallback#278

Merged
viettranx merged 2 commits into
nextlevelbuilder:mainfrom
badgerbees:feature/telegram-networking-isolation
Mar 19, 2026
Merged

feat(telegram): networking isolation and sticky IPv4 fallback#278
viettranx merged 2 commits into
nextlevelbuilder:mainfrom
badgerbees:feature/telegram-networking-isolation

Conversation

@badgerbees
Copy link
Copy Markdown
Contributor

Summary

This PR addresses the recent regression in Telegram reliability (intermittent sendMessage failures and "Network request failed" logs) by refactoring the networking stack from a shared global dispatcher to a per-account isolated transport model.

The Problem

The Telegram networking implementation was previously depending on a shared global dispatcher state for proxy handling, IPv4/IPv6 selection, and DNS order.
When a fallback occurred for one bot, it wasn't effectively retained or isolated. This led to a "cross-contamination" effect where a connectivity failure (like broken IPv6 routing on a VPS) for one account would disrupt all other bot instances, causing them to retry fallback behavior far more often than expected.

The Solution

  • Networking Isolation: Each bot instance now uses its own http.Transport (cloned from http.DefaultTransport). This isolates connection pools, proxies, and dialer settings per bot account.
  • Sticky IPv4 Fallback: Implemented a "sticky" fallback mechanism. If a bot detects a connectivity error (e.g., network unreachable or timeout) potentially related to IPv6 routing, it automatically forces its dialer into tcp4 mode for all future requests in the current session.
  • Overall Timeout Budget: Replaced per-attempt timeouts with a unified 60-second overall budget for the entire retrySend sequence. This prevents deterministic hangs on slow or flapping networks.
  • Unified Pathing: Ensured the isolated bot client is correctly used for all operations: long-polling, message sends (text and media), and large file downloads.

badgerbees and others added 2 commits March 19, 2026 14:34
- Tune MaxIdleConnsPerHost to 64 (default 2 starves concurrent sends)
- Fix data race in enableIPv4Only using sync.Once
- Add force_ipv4 config option for explicit IPv4-only mode
- Narrow auto-detect heuristic to "unreachable" only (avoid false-positive on timeouts)
- Use bot username instead of token prefix in logs
- Extract applyIPv4Dialer helper for reuse between config and runtime paths
@viettranx viettranx merged commit 3f94012 into nextlevelbuilder:main Mar 19, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants